package org.example.bobofish.user.common.util;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class CacheUtil<T> {

    @Autowired
    private StringRedisTemplate redisTemplate;

    public void cacheData(String key, T value){
        redisTemplate.opsForValue().set(key, JSON.toJSONString(value));
    }

    public void cacheDataExpire(String key, T value, long time, TimeUnit timeUnit){
        redisTemplate.opsForValue().set(key, JSON.toJSONString(value), time, timeUnit);
    }

    public String getCachedData(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    public void removeCachedData(String key) {
        redisTemplate.delete(key);
    }
}
